<template>
	<view class="background">
		<navigator open-type="navigateBack" :delta="1" url="">
			<view class="back">
				<i class="iconfont iconzuojiantou"></i>
			</view>
		</navigator>
		<view class="logo logo-margin"></view>
		<view class="flex size-input">
			<view class="text">
				昵称
			</view>
			<input type="text" value="" @input="getNickname" maxlength="8" placeholder-class="holder" placeholder="请输入用户名" />
		</view>
		<view class="flex size-input margin">
			<view class="text">
				密码
			</view>
			<input type="password" value="" @input="getPassword" maxlength="20" placeholder-class="holder" placeholder="请输入密码" />
		</view>
		<view class="flex size-input margin">
			<view class="text">
				确认密码
			</view>
			<input type="password" value="" @blur="getRePassword" maxlength="20" placeholder-class="holder" placeholder="请确认密码" />
		</view>
		<view class="flex size-input margin">
			<view class="text">
				手机号
			</view>
			<input type="number" value="" @input="getPhone" maxlength="13" placeholder-class="holder" placeholder="请输入手机号" />
		</view>
		<view class="flex size-input margin">
			<view class="text">
				验证码
			</view>
			<view class="border flex">
				<input type="number" class="border-none" @input="getVerificationCode" value="" maxlength="6" placeholder-class="holder" placeholder="请输入验证码" />
				<view class="code" @click="getCode">
					获取验证码
				</view>
			</view>
		</view>
		<view class="flex size-input margin">
			<view class="text">
				邀请人
			</view>
			<input type="text" disabled="true" v-model="inviter" maxlength="20" placeholder-class="holder" placeholder="请输入邀请码" />
		</view>
		<view class=" btn" @click="toRegsiter">
			注册
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname:'',
				password:'',
				repassword:'',
				phone:'',
				code:'',
				inviter:'',
				flag:true
			}
		},
		methods: {
			onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
				this.inviter = option.code; //打印出上个页面传递的参数。
			},
			
			getCode(){
				if (this.phone != '') {
					this.req.request('/regist/sendRegistSms', '', 'POST', {
						mobile: this.phone
					}).then((res) => {
						if (res == undefined) {
							uni.showToast({
								title: '响应超时',
								icon: "none",
								mask: true,
				
							})
						} else if (res.data.code == '500') {
							this.req.toast(res.data.message)
						} else if (res.data.code == '200') {
							uni.showModal({
								title: "验证码已发送",
								success: (res) => {
									console.log(res)
								}
							})
						}
					})
				} else {
					uni.showToast({
						title: '注册手机号未输入',
						icon: 'none'
					})
				}
			},
			
			getPhone(e) {
				this.phone = e.target.value
				// console.log(e)
			},
			getNickname(e){
				this.nickname = e.target.value
			},
			getPassword(e){
				this.password = e.target.value
			},
			getRePassword(e){
				this.repassword = e.target.value
				if(this.password != this.repassword){
					this.flag = false
					uni.showToast({
						title: '重复密码不正确',
						icon: 'none'
					})
				}
			},
			getVerificationCode(e){
				this.code = e.target.value
			},
			
			toRegsiter(){
				if(this.flag){
					this.req.request('/regist/regist', '', 'POST', {
						nickname:this.nickname,
						password:this.password,
						mobile:this.phone,
						verificationCode:this.code,
						inviter:this.inviter,
					}).then((res) => {
						if (res == undefined) {
							uni.showToast({
								title: '响应超时',
								icon: "none",
								mask: true,
									
							})
						} else if (res.data.code == '500') {
							this.req.toast(res.data.message)
						} else if (res.data.code == '200') {
							uni.showModal({
								title: "验证码已发送",
								success: (res) => {
									console.log(res)
								}
							})
						}
					})
				}
			}
		}
		
	}
</script>

<style>
	.iconfont {
		font-size: 64rpx;
	}

	.code {
		font-weight: bold;
		text-align: center;
		width: 340rpx;
		font-size: 28rpx;
		color: #F0AD4E;
	}

	.border-none {
		border: none;
	}

	.margin {
		margin-top: 60rpx !important;
	}

	.btn {
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		height: 80rpx;
		margin-top: 100rpx;
		background: #F0AD4E;
		color: white;
		line-height: 80rpx;
		border-radius: 20rpx;
	}

	.forget-password {
		width: 180rpx;
		font-size: 28rpx;
		margin-top: 30rpx;
		text-align: center;
		margin-left: calc(100% - 180rpx);
	}

	.holder {
		color: rgb(195,196,200);
		font-size: 28rpx;
	}

	.border {
		width: 100%;
		border-bottom: 1px solid rgb(248,248,248);
	}

	input {
		/* border-radius: 12rpx; */
		outline: none;
		box-sizing: border-box;
		height: 100%;
		width: 100%;
		padding-left: 20rpx;
		border-bottom: 1px solid rgb(248,248,248);
	}

	.size-input {
		line-height: 60rpx;
		width: 100%;
		height: 60rpx;
		margin-top: 120rpx;
	}

	.text {
		font-weight: bold;
		color: rgb(195,196,200);
		width: 160rpx;
		font-size: 28rpx;
	}

	.logo-margin {
		margin: 160rpx 0px 0px;
	}
</style>
